var api = require('../../utils/api.js')
var $ = require('../../utils/util.js')
var app = getApp()
var regeneratorRuntime = require('../../utils/runtime.js')
Component({
  properties: {
    formData:{
      value:'',
    },
    showType: { // 类型  1：上传   2：显示
      type: String,
      value: '1',
    },
    url: { // 显示模式下  图片路径
      type: String,
      value: '',
    },
    pics: { // 图片资源
      type: Array,
      value: [],
    },
    isSet: {// 是否是添加
      type: Boolean,
      value: true
    },
    max:{ // 最大上传图片的数量
      type: Number,
      value: 9
    },
    key:{ // 字段（后端要用）
      type: String,
      value:'key'
    },
    name: { // 上传组件下面要显示的字
      type: String,
      value:'添加图片'
    }

  },
  attached: function () {

  },
  data: {
    o_img_w: 100, // 一个图片的宽度
  },
  observers: {
    'pics': function (pics) {
    }
  },
  methods: {
    upPicsHandle() { //上传宣传照片 
            if (this.data.pics.length == this.data.max) {
              $.toast(`只能选择${this.data.max}张`)
              return
            }
            var count = this.data.max - this.data.pics.length
            wx.chooseImage({
              count, // 默认9
              sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
              sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
              success: (res) => {
                const srcArray = res.tempFilePaths 
                var len = this.data.pics.length
                srcArray.forEach((item, index) => {
                  $.uploadFile(api.main.uploadOptImg, item, 'image',{ticketNumber:this.data.formData}).then((res) => {
                    var pics =  this.data.pics 
                    pics[index + len] = res.msg
                    this.setData({ pics })  
                    this.triggerEvent('upPicsHandle', { pics: this.data.pics, key: this.data.key })
                  })
                })
              }
            })
          
    },

    preImgHandle(e) {
      var idx = e.currentTarget.dataset.idx;
      $.previewImage(this.data.pics, this.data.pics[idx])
    },

    preImgHandle1(e) {
      var urls=[this.data.url];
      $.previewImage(urls)

    },

    deletPhotoHandle(e) { //删除某个图片
     
          var idx = e.currentTarget.dataset.idx; 
          var pics = this.data.pics;
          pics.splice(idx, 1);
          this.setData({
            pics,
          })
        
          this.triggerEvent('upPicsHandle', { pics: this.data.pics, key: this.data.key })
        
    }

  }
})
